from enum import StrEnum
from typing import Optional

import flask_login
from pydantic import BaseModel

from models.enums import AccountStatus, Gender


class AccountRole(StrEnum):
    SUPER_ADMIN = "super"
    ENTERPRICE = "enterprise"
    TENANT_OWNER = "owner"  # ==Owner
    TENANT_ADMIN = "admin"  # ==Admin
    # D_TENANT_EDITOR = "editor"  # ==Editor
    NORMAL_USER = "normal"  # ==Normal
    # D_DATASET_OPERATOR = "dataset_operator"  # ==DatasetOperator
    # AGENT_CREATOR = "creator"
    # MOUSE_USER = "mouse"
    AGENT_USER = "agent"

    @classmethod
    def is_privileged_to_role(cls, my_role: str, target_role: str) -> bool:
        my_prior = ROLE_PRIORITY.get(my_role, None)
        target_prior = ROLE_PRIORITY.get(target_role, None)
        if my_prior is None or target_prior is None:
            return False
        return my_prior >= target_prior
    

    @staticmethod
    def is_privileged_role(role: str) -> bool:
        if not role:
            return False  # LobyLiang
        return role in {AccountRole.TENANT_OWNER, AccountRole.TENANT_ADMIN, AccountRole.SUPER_ADMIN, AccountRole.ENTERPRICE}



ROLE_PRIORITY = {
    AccountRole.SUPER_ADMIN.value: 20,
    AccountRole.ENTERPRICE.value: 15,
    AccountRole.TENANT_OWNER.value: 9,
    AccountRole.TENANT_ADMIN.value: 8,
    # AccountRole.D_TENANT_EDITOR.value: 7,
    AccountRole.NORMAL_USER.value: 5,
    # AccountRole.D_DATASET_OPERATOR.value: 4,
    # AccountRole.AGENT_CREATOR.value: 3,
    # AccountRole.MOUSE_USER.value: 1,
    AccountRole.AGENT_USER.value: 1,
}

class AccountWrap(flask_login.UserMixin,BaseModel):
    id: str
    name: str
    gender:Gender|None = None
    avatar: Optional[str] = None
    group_id: Optional[str] = None
    open_id: Optional[str] = None
    current_tenant_id: str
    roles: list[str] = []
    status: AccountStatus|None=None
    timezone:str = "Asia/Shanghai"
    interface_language:str= "zh-CN"